package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.mongo.UserLocationDTO;
import com.tanhua.domain.vo.AddressVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;


import java.util.List;

@Service
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Double latitude, Double longitude, String address, long userId) {
        //1. 判断MongoDB中有没有用户的地址位置信息
        Query query = new Query(Criteria.where("userId").is(userId));
        if (mongoTemplate.exists(query, UserLocation.class)) {
            //数据库中已有用户的地理位置，更新
            GeoJsonPoint point = new GeoJsonPoint(longitude, latitude);
            Update update = new Update()
                    .set("address", address)
                    .set("updated", System.currentTimeMillis())
                    .set("location", point);
            mongoTemplate.updateFirst(query, update, UserLocation.class);
        } else{
            //数据库中没有用户的地址位置，保存
            UserLocation userLocation = new UserLocation();
            userLocation.setId(ObjectId.get());
            userLocation.setUserId(userId);
            userLocation.setAddress(address);
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setUpdated(System.currentTimeMillis());
            userLocation.setLastUpdated(System.currentTimeMillis());
            userLocation.setLocation(new GeoJsonPoint(longitude, latitude));

            mongoTemplate.save(userLocation);
        }
    }


    @Override
    public List<UserLocationDTO> searchNear(long userId, long metre) {
        //1. 查询用户的位置
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);

        //2. 以用户位置为圆点，设置半径，划一个圆作为搜索范围
        GeoJsonPoint point = userLocation.getLocation();
        Distance distance = new Distance(metre/1000, Metrics.KILOMETERS);
        Circle circle = new Circle(point, distance);

        //3. 搜索指定范围内的人
        Query nearQuery = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = mongoTemplate.find(nearQuery, UserLocation.class);
        return UserLocationDTO.translateList(userLocations);
    }

    @Override
    public AddressVo findLocation(Long userId) {
        //1. 查询用户的位置
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        double x = location.getLocation().getX();
        double y = location.getLocation().getY();
        String address = location.getAddress();
        AddressVo addressVo = new AddressVo(x,y,address);
        return addressVo;
    }
}